package com.sodaopen.study.service.impl;

import com.sodaopen.study.entity.Chapter;
import com.sodaopen.study.mapper.ChapterMapper;
import com.sodaopen.study.mapper.CourseMapper;
import com.sodaopen.study.service.ChapterService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChapterServiceImpl implements ChapterService {

	@Autowired
	private ChapterMapper chapterMapper;

	@Autowired
	private CourseMapper courseMapper;

	@Override
	public List<Chapter> findLevelChapterByCourseId(Integer id) {
		List<Chapter> list = chapterMapper.selectByCourseId(id);
		
		return convertToLevelList(list);
	}

	@Override
	public List<Chapter> findLevelChapterByCourseIdFinish(Integer courseId, Integer userId) {
		List<Chapter> list = chapterMapper.selectByCourseIdFinish(courseId, userId);
		return convertToLevelList(list);
	}

	@Override
	public Map findLockedByPrimaryKey(Integer userId, Integer courseId) {
		return chapterMapper.selectLockedByPrimaryKey(userId, courseId);
	}

	@Override
	public void lockUserChapter(Integer userId, Integer courseId, Integer chapterId) {
		chapterMapper.insertLockedRecord(userId, courseId, chapterId);
	}

	@Override
	public List<Chapter> findByCourseId(Integer courseId) {
		List<Chapter> list = chapterMapper.selectByCourseId(courseId);
		for(Chapter c : list){
			if(c.getPid() == null){
				c.setPid(-1);
			}
		}
		return list;
	}

	@Override
	public Integer findMaxSort(Integer pid, Integer courseId) {
		return chapterMapper.selectMaxSort(pid, courseId);
	}

	@Override
	public void saveChapter(Chapter chapter) {
		chapterMapper.insertChapter(chapter);
	}

	/**
	 * 将章节列表按层级关系组装
	 * @param list
	 * @return
	 */
	private List<Chapter> convertToLevelList(List<Chapter> list){
		Map<Integer, Chapter> map = new HashMap<>();
		for(Chapter c : list) {
			map.put(c.getId(), c);
		}

		//按层级组装章节
		Chapter root = new Chapter();
		for(Chapter c : list) {
			if(c.getPid() == null) {
				root.addChild(c);
			}else {
				map.get(c.getPid()).addChild(c);
			}
		}

		return root.getChildren();
	}

	@Override
	public Chapter findById(Integer id) {
		return chapterMapper.selectById(id);
	}

	/**
	 * 返回第一个未完成的章节
	 * 如果所有章节都已经完成，则返回第一个章节
	 */
	@Override
	public Chapter findCurrentChapter(List<Chapter> chapterList) {
		for(Chapter c : chapterList) {
			if(c.getChildren() != null) {
				for(Chapter child : c.getChildren()) {
					if("N".equals(child.getFinished())) {
						return child;
					}
				}
			}
		}
		
		return chapterList.get(0).getChildren().get(0);  //没有未完成的，则从头开始
	}

	@Override
	@Transactional
	public void finishChapter(Integer userId, Integer chapterId) {
		//完成该节
		chapterMapper.insertFinishRecord(userId, chapterId);

		//完成该节对应的章
		Chapter chapter = chapterMapper.selectById(chapterId);
		if(chapter.getPid() != null){
			Integer unfinishCount = chapterMapper.selectUnfinishCount(userId, chapter.getPid());
			if(unfinishCount == 0) {
				chapterMapper.insertFinishRecord(userId, chapter.getPid());

				//完成对应的课程
				Integer unfinishCourse = chapterMapper.selectUnfinishCourse(userId, chapter.getCourseId());
				if(unfinishCourse == 0){
					courseMapper.insertFinishRecord(userId, chapter.getCourseId());
				}
			}
		}
	}

	@Override
	public Chapter findByIdFinish(Integer id, Integer userId) {
		return chapterMapper.selectByIdFinish(id, userId);
	}


}
